// (c) Copyright 2013-2014 Stephen Gold <sgold@sonic.net>
// Distributed under the terms of the GNU General Public License

/*
 This file is part of the Bats Game.

 The Bats Game is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by the
 Free Software Foundation, either version 3 of the License, or (at your
 option) any later version.

 The Bats Game is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.

 You should have received a copy of the GNU General Public License
 along with the Bats Game.  If not, see <http://www.gnu.org/licenses/>.
 */
package bats.ui;

import java.util.logging.Logger;
import jme3utilities.MyString;
import jme3utilities.ui.Hotkey;
import jme3utilities.ui.InputMode;

/**
 * An item in the hotkey listbox of the hotkey binding screen.
 *
 * @author Stephen Gold <sgold@sonic.net>
 */
class HotkeyItem {
    // *************************************************************************
    // constants

    /**
     * message logger for this class
     */
    final private static Logger logger =
            Logger.getLogger(HotkeyItem.class.getName());
    // *************************************************************************
    // fields
    /**
     * which input mode (not null)
     */
    final private InputMode mode;
    /**
     * which hotkey (not null)
     */
    final private Hotkey hotkey;
    // *************************************************************************
    // constructors

    /**
     * Instantiate an item based on its hotkey and an input mode.
     *
     * @param hotkey (not null, set by constructor)
     * @param mode (not null, set by constructor)
     */
    HotkeyItem(Hotkey hotkey, InputMode mode) {
        assert hotkey != null;
        assert mode != null;

        this.hotkey = hotkey;
        this.mode = mode;
    }
    // *************************************************************************
    // new methods exposed

    /**
     * Access the hotkey.
     */
    Hotkey getHotkey() {
        return hotkey;
    }

    /**
     * Test whether the hotkey is bound.
     *
     * @return true if the hotkey is bound, else false
     */
    boolean isBound() {
        boolean result = mode.binds(hotkey);
        return result;
    }

    /**
     * Test whether this item corresponds to the specified key code.
     *
     * @param keyCode
     * @return true if it corresponds, else false
     */
    boolean isForKeyCode(int keyCode) {
        int thisCode = hotkey.keyCode();
        boolean result = thisCode == keyCode;
        return result;
    }
    // *************************************************************************
    // Object methods

    /**
     * Convert the item to a string so the listbox can display it.
     *
     * @return display string for the item (not null)
     */
    @Override
    public String toString() {
        if (mode == null) {
            return "";
        }
        String name = hotkey.name();
        String result = String.format(" [ %s ]    ... ", name);
        if (isBound()) {
            String action = mode.getActionName(hotkey);
            result += String.format("bound to %s", MyString.quote(action));
        } else {
            result += "not bound";
        }
        return result;
    }
}